<?php

namespace App\Kuafu\WaiMai\Channel;

use App\Kuafu\Shop\Service\DouyinLibrary\DouyinShopService;
use App\Kuafu\Shop\Service\DouyinLibrary\DouyinWaiMaiService;
use GuzzleHttp\Exception\GuzzleException;

class DyWaiMai extends BaseWaiMai
{
    const TIME_OUT               = 10;
    const SAND_BOX_CLIENT_KEY    = 'sandbox009'; // 沙箱client_key
    const SAND_BOX_CLIENT_SECRET = '111111'; // 沙箱client_secret
    const SAND_BOX_TOKEN         = 'douyin.hC5srEMwjDUY83MbtMrUtrwqW2yLIbai05A6HqzQnPc0vBjKfpGz2obcaYLR'; // 沙箱token
    const SAND_BOX_ACCOUNT_ID    = '7165833310558488616'; // 沙箱商户Id
    const SAND_BOX_ACCOUNT_NAME  = '深圳汇艺谷网络科技有限公司'; // 沙箱商户名称
    const CLIENT_KEY             = 'awqanb3e6n0udkco'; // client_key
    const CLIENT_SECRET          = 'fef0bbb2ac1c71d8886566e85f506d99'; // client_secret
    const ACCOUNT_ID             = '7089001835192649766'; // 商户Id
    const ACCOUNT_NAME           = '北京膳鸿餐饮管理有限公司'; // 商户名称


    private string $host       = 'https://open.douyin.com';
    private string $app_id     = '';
    private string $app_secret = '';
    private string $thirdShopId;
    private int    $shopId;

    public function __construct(string $thirdShopId, int $shopId)
    {
        parent::__construct();
        /*if (!$thirdShopId) {
            $thirdShopId = self::SAND_BOX_ACCOUNT_ID;
        }
        if (!$shopId) {
            $shopId = 1;
        }*/
        $this->thirdShopId = $thirdShopId;
        $this->shopId      = $shopId;
    }

    /**
     * 查询单个店铺详情
     * @return array
     */
    public function getShop(): array
    {
        $shopInfo = (new DouyinShopService)->getShopByPoiId($this->thirdShopId);
        if ($shopInfo['error_code'] != 0) {
            return [];
        }
        $result    = $shopInfo['pois'][0]['poi'];
        $s['data'] = [
            'name'   => $result['poi_name'] ?? '',
            'phones' => [''],
        ];
        return $s;
    }

    /**
     * 订单详情
     * @param string $orderId
     * @param string $accountId
     * @return array
     * @throws GuzzleException
     */
    public function getOrderDetail(string $orderId, string $accountId = self::ACCOUNT_ID): array
    {
        $param = [
            'order_id'   => $orderId,
            'account_id' => $accountId,
        ];
        return (new DouyinWaiMaiService)->queryOrder($param);
    }

    /**
     * 商家出餐
     * @param int $orderId
     * @return array|mixed
     */
    public function finishFoods(int $orderId): array
    {
        return [
            'status'  => true, // true: 接口成功 false: 接口失败
            'code'    => 200, // 错误码
            'msg'     => '',  // 错误信息
            'data'    => [],   // 多种支付渠道合并后的，固定通用返回字段
            'oriData' => [], //
        ];
    }

}
